// v2010.09.19

/*
Copyright (c) 2010 Ben Hendel-Doying

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

package game;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsEnvironment;
import java.awt.image.BufferedImage;
import java.net.URL;

import javax.imageio.ImageIO;

import resources.Sprite;
import speaker.Speaker;
import world.Rectangle;

public class ASCII
{
	private static ASCII single = new ASCII();

	public static final int COLOR_BLACK = 0;
	public static final int COLOR_LIGHT_GRAY = 1;
	public static final int COLOR_DARK_GRAY = 2;
	public static final int COLOR_WHITE = 3;

	public static final int COLOR_DARK_RED = 4;
	public static final int COLOR_RED = 5;
	public static final int COLOR_PURPLE = 6;
	public static final int COLOR_MAGENTA = 7;

	public static final int COLOR_DARK_GREEN = 8;
	public static final int COLOR_GREEN = 9;
	public static final int COLOR_DARK_YELLOW = 10;
	public static final int COLOR_YELLOW = 11;

	public static final int COLOR_DARK_BLUE = 12;
	public static final int COLOR_BLUE = 13;
	public static final int COLOR_DARK_CYAN = 14;
	public static final int COLOR_CYAN = 15;

	private static final Color colors[] = {
		new Color(  0,   0,   0), new Color(192, 192, 192), new Color(128, 128, 128), new Color(255, 255, 255),
		new Color(128,   0,   0), new Color(255,   0,   0), new Color(128,   0, 128), new Color(255,   0, 255),
		new Color(  0, 128,   0), new Color(  0, 255,   0), new Color(128, 128,   0), new Color(255, 255,   0),
		new Color(  0,   0, 128), new Color(  0,   0, 255), new Color(  0, 128, 128), new Color(  0, 255, 255),
	};

	public static final int SPRITE_ASCII_TABLE = 0;

	private static Sprite ASCII_table;
	
	public static ASCII get() { return single; }

	private ASCII()
	{
		try
		{
			BufferedImage sourceImage = null;

			URL url = getClass().getResource("/gfx/asciitable.png");
			sourceImage = ImageIO.read(url);

			GraphicsConfiguration gc = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
			BufferedImage image = gc.createCompatibleImage(sourceImage.getWidth(), sourceImage.getHeight(), sourceImage.getTransparency());

			Graphics g = image.getGraphics();
			g.drawImage(sourceImage, 0, 0, null);
			g.dispose();

			ASCII_table = new Sprite(image);
		}
		catch (Exception e)
		{
			Speaker.Say(this, "Failed to load graphics.");
			System.exit(1);
		}
	}

	public static void DrawCharacter(Graphics g, char c, int color, int x, int y)
	{
		g.setColor(colors[color]);
		g.fillRect(x * 20, y * 20, 20, 20);

		ASCII_table.Draw(g, x * 20, y * 20, (c % 16) * 20, (int)(c / 16) * 20, 20, 20);
	}

	public static void DrawString(Graphics g, String s, int color, int x, int y)
	{
		int len = s.length();
		
		g.setColor(colors[color]);
		g.fillRect(x * 20, y * 20, len * 20, 20);

		for(int i = 0; i < len; ++i)
		{
			char c = s.charAt(i);

			ASCII_table.Draw(g, (x + i) * 20, y * 20, (c % 16) * 20, (int)(c / 16) * 20, 20, 20);
		}
	}
	
	public static void DrawBox(Graphics g, int color, int x1, int y1, int w, int h)
	{
		g.setColor(colors[color]);

		// top line
		g.fillRect(x1 * 20, y1 * 20, w * 20, 20);
		
		// top corners
		ASCII_table.Draw(g, x1 * 20, y1 * 20, (213 % 16) * 20, (int)(213 / 16) * 20, 20, 20);
		ASCII_table.Draw(g, (x1 + w - 1) * 20, y1 * 20, (184 % 16) * 20, (int)(184 / 16) * 20, 20, 20);

		// bottom line
		g.fillRect(x1 * 20, (y1 + h - 1) * 20, w * 20, 20);

		// bottom corners
		ASCII_table.Draw(g, x1 * 20, (y1 + h - 1) * 20, (212 % 16) * 20, (int)(212 / 16) * 20, 20, 20);
		ASCII_table.Draw(g, (x1 + w - 1) * 20, (y1 + h - 1) * 20, (190 % 16) * 20, (int)(190 / 16) * 20, 20, 20);

		// top and bottom lines
		for(int x = x1 + 1; x < x1 + w - 1; ++x)
		{
			ASCII_table.Draw(g, x * 20, y1 * 20, (205 % 16) * 20, (int)(205 / 16) * 20, 20, 20);
			ASCII_table.Draw(g, x * 20, (y1 + h - 1) * 20, (205 % 16) * 20, (int)(205 / 16) * 20, 20, 20);
		}
		
		// left and right line
		g.fillRect(x1 * 20, (y1 + 1) * 20, 20, (h - 2) * 20);
		g.fillRect((x1 + w - 1) * 20, (y1 + 1) * 20, 20, (h - 2) * 20);
		
		for(int y = y1 + 1; y < y1 + h - 1; ++y)
		{
			ASCII_table.Draw(g, x1 * 20, y * 20, (179 % 16) * 20, (int)(179 / 16) * 20, 20, 20);
			ASCII_table.Draw(g, (x1 + w - 1) * 20, y * 20, (179 % 16) * 20, (int)(179 / 16) * 20, 20, 20);
		}
	}

	public static void DrawBox(Graphics g, int color, Rectangle r)
	{
		DrawBox(g, color, r.GetX(), r.GetY(), r.GetWidth(), r.GetHeight());
	}

	public static void DrawVerticalScrollBar(Graphics g, int cur_value, int max_value, int color, int x, int y, int h)
	{
		ASCII.DrawCharacter(g, (char)24, color, x, y);
		ASCII.DrawCharacter(g, (char)25, color, x, y + h - 1);

		int pos = cur_value * (h - 3) / max_value;

		ASCII.DrawCharacter(g, (char)219, color, x, pos + y + 1);
	}
	
	public static void DrawHorizontalLine(Graphics g, int color, int x, int y, int w)
	{
		g.setColor(colors[color]);

		g.fillRect(x * 20, y * 20, (x + w - 1) * 20, 20);

		for(int i = x; i < x + w; ++i)
			ASCII_table.Draw(g, i * 20, y * 20, (196 % 16) * 20, (int)(196 / 16) * 20, 20, 20);
	}
	
	public static void DrawHorizontalBreak(Graphics g, int color, int y)
	{
		ASCII.DrawCharacter(g, (char)195, color, 0, y);
		ASCII.DrawHorizontalLine(g, color, 1, y, 38);
		ASCII.DrawCharacter(g, (char)180, color, 39, y);
	}
}
